﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;
using LowLevelGraphics.CustomControls;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// 
    /// </summary>
    public class ActionTest : AbstractCommand
    {
        private SelectablePictureBox m_PictureBox1 = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_pictureBox"></param>
        public ActionTest(SelectablePictureBox _pictureBox)
        {
            m_PictureBox1 = _pictureBox;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Run(object sender, EventArgs e)
        {
            int n = 0;
            string[] aFile = Directory.GetFiles(@"g:\temp\ebooks\wbl\gallery", "*.jpg", SearchOption.TopDirectoryOnly);
            string sResultText = string.Empty;


            BinaryCoder coder = new BinaryCoder();
            foreach (string sFile in aFile)
            {
                Image imgDecoded = coder.GetAsImage(sFile);
                Bitmap bmpTemp = (Bitmap)imgDecoded;
                Size szDouble = new Size(bmpTemp.Size.Width / 4, bmpTemp.Size.Height / 4);
                bmpTemp = BitmapFunctions.Resize((Bitmap)bmpTemp, szDouble);
                new FastSegmentation(FASTSEGMENTATION_MODE.HORIZONTAL, 1).Execute(bmpTemp);
                float fPercentage = 0.0f;

                m_PictureBox1.Invoke(new MethodInvoker(delegate()
                {
                    m_PictureBox1.BackgroundImage = bmpTemp;
                    m_PictureBox1.Invalidate();
                    m_PictureBox1.Update();
                }));

                if (bmpTemp == null) continue;
                List<ColorRect> aColorRect = ImageFilters.FindBRectangles2(bmpTemp, out fPercentage);
                Graphics graphics = Graphics.FromImage(bmpTemp);
                if (fPercentage > 0.0f)
                {
                    sResultText = "Result: Wide belt found";
                    string sCopyFileName = @"c:\temp\Wide belt\" + Path.GetFileName(sFile);
                    if (!File.Exists(sCopyFileName))
                    {
                        File.Copy(sFile, sCopyFileName);
                    }
                }
                else
                {
                    sResultText = "NO BELT!";
                    string sCopyFileName = @"c:\temp\No Wide belt\" + Path.GetFileName(sFile);
                    if (!File.Exists(sCopyFileName))
                    {
                        File.Copy(sFile, sCopyFileName);
                    }

                    Font font = new Font("Arial", 35, FontStyle.Bold);
                    graphics.DrawString(sResultText, font, new SolidBrush(Color.Red), bmpTemp.Width / 2 - graphics.MeasureString(sResultText, font).Width / 2, bmpTemp.Height / 2);
                    font.Dispose();
                }
                foreach (ColorRect rect in aColorRect)
                {
                    graphics.FillRectangle(new SolidBrush(Color.FromArgb(127, rect.Color)), rect.Rectangle);
                }
                aColorRect.Clear();


                m_PictureBox1.Invoke(new MethodInvoker(delegate()
                    {
                        m_PictureBox1.BackgroundImage = bmpTemp;
                        m_PictureBox1.Invalidate();
                        m_PictureBox1.Update();
                    }));
                graphics.Dispose();
                imgDecoded.Dispose();
            }
        }
    }
}
